package com.grupit.engine.pluggablefeatures.domain;

import java.io.IOException;
import java.util.List;

import javax.jdo.annotations.Column;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Version;
import javax.jdo.annotations.VersionStrategy;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.grupit.engine.GrupitEngineConnector;
import com.grupit.engine.GrupitEngineFacade;
import com.grupit.engine.exception.GrupitEngineException;
import com.grupit.engine.service.NoteService;

import es.chaveweb.poZit.server.domain.PoZit;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Inheritance(strategy = InheritanceStrategy.NEW_TABLE)
@Version(column = "version", strategy = VersionStrategy.VERSION_NUMBER, extensions = { @Extension(vendorName = "datanucleus", key = "field-name", value = "version") })
public class Note extends PoZit {
	private static NoteService poZitService;

	@NotNull(message = "La nota debe tener un propietario.")
	private String ownerUserId;

	@NotNull
	@Column(length = 51)
	@Size(max = 51, message = "El 'SystemId' del destinatario no puede sobrepasar los 51 carácteres, de lo contrario es un SystemID inválido")
	private String targetSystemId;

	protected long version;

	public Long getId() {
		return getNoteId();
	}

	public String getOwnerUserId() {
		return ownerUserId;
	}

	public void setOwnerUserId(String ownerUserId) {
		this.ownerUserId = ownerUserId;
	}

	public String getTargetSystemId() {
		return targetSystemId;
	}

	public void setTargetSystemId(String targetSystemId) {
		this.targetSystemId = targetSystemId;
	}

	public void save() throws GrupitEngineException, IOException {
		NoteService poZitService = ensurePoZitService();
		poZitService.save(this);
	}

	public void delete() throws GrupitEngineException, IOException {
		NoteService poZitService = ensurePoZitService();
		poZitService.delete(this);
	}

	public static Note findNote(Long noteId) throws GrupitEngineException, IOException {
		NoteService poZitService = ensurePoZitService();
		Note note = poZitService.findPoZitById(noteId);
		return note;
	}

	public static List<Note> findAllNotesByUserId(String userId) throws GrupitEngineException, IOException {
		NoteService poZitService = ensurePoZitService();
		List<Note> notes = poZitService.findAllNotesByUserId(userId);
		return notes;
	}

	protected static NoteService ensurePoZitService() throws GrupitEngineException {
		if (poZitService == null) {
			GrupitEngineFacade engine = GrupitEngineConnector.getGrupitEngineFacade();
			poZitService = engine.getRegisteredPluggableFeatureService(NoteService.class);
		}
		return poZitService;
	}

	public Long getVersion() {
		return version;
	}
}
